﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

public partial class admin_manager_managerList : BaseAdmin
    {
    protected string kw = "";
    protected int ot = 0;
        protected int pageCount = 0;
        protected string pid = "";
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected string order = "AdminName";
        protected PageList pl = new PageList();

     
        protected int t = 0;
        protected int total = 0;
        bool _isEdit = false;
        bool _isDeleted = false;
        string _addDisplay = "none";
        bool _isPermission = false;

        public string AddDisplay
        {
            get { return _addDisplay; }
            set { _addDisplay = value; }
        }

    

        private void DepartmentBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }
                if (base.Request.QueryString["ot"] != null)
                {
                    this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
                }
                if (base.Request.QueryString["pid"] != null)
                {
                    this.pid = base.Request.QueryString["pid"];
                }
                this.pl.TblName = "tbAdminlogin";
                this.pl.StrGetFields = " AdminId, AdminUser, AdminPwd, AdminName, AdminFlag, AdminDesc, LoginDegree, LoginIP, Logintime, InphoneNumber, DepartmentID, IsDeleted, AdminTypeId, RoleNames = stuff((select ',' +  RoleName from tbRole R join tbAdminRoleRef AR on R.Id = AR.RoleId and isnull(R.IsDeleted,0) = 0 and AR.AdminId = tbAdminlogin.AdminId for xml path('')) , 1 , 1 , '')"
                                       + ", (select DepName from tbDepartment where id=tbAdminlogin.DepartmentId) as DepName ";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
                this.pl.StrWhere = " isnull(IsDeleted,0) = 0 and isnull(AdminTypeId,0) = 0 and departmentid in (select id from tbDepartment where isnull(IsDeleted,0) = 0 and  companyId=" + base.Administrator.CompanyId + ") ";
                if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
                {
                    this.kw = base.Request.QueryString["kw"];
                    this.pl.StrWhere = this.pl.StrWhere + " and " + base.Request.QueryString["t"] + " like '%" + this.kw + "%' ";
                }

                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                if (pageList.Rows.Count == 0)
                {
                    this.btnDelete.Visible = false;
                }
                this.rptManager.DataSource = pageList.DefaultView;
                this.rptManager.DataBind();
            }
            catch (Exception ex)
            {
            }
        }


        void ValidateLoginUser()
        {
            if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.ManagerList))
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.ManagerAdd))
            {
                _addDisplay = "block";
            }
            if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.ManagerEdit))
            {
                _isEdit = true;
            }
            if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.ManagerDeleted))
            {
                _isDeleted = true;
            }
            if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.ManagerRole))
            {
                _isPermission = true;
            }
        }

    

  

        protected void rptManager_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            base.SetListPerssion(e,_isEdit, _isDeleted,_isPermission);

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!base.IsPostBack)
            {
                this.ValidateLoginUser();
                this.DeleteItem();
                this.DepartmentBind();
            }
        }

        void DeleteItem()
        {
            if (Request.QueryString["action"] == "del" && !string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                string str = Request.QueryString["id"];
                BaseFactory.theAdmin.Delete(int.Parse(str));
                base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除成功！');</script>");
            }
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string str = base.Request.Form["Item"];
                
            }
            catch
            {
            }

            base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
        }


    }
